﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Common.Infrastructrue
{
    /// <summary>
    /// 仓储基类
    /// </summary>
    public class BaseResponstity<T> : IBaseResponstity<T> where T : class, new()
    {
        private readonly DbCommontext db;
        private readonly ILogger<BaseResponstity<T>> logger;

        public BaseResponstity(DbCommontext db, ILogger<BaseResponstity<T>> logger)
        {
            this.db = db;
            this.logger = logger;
        }

        public async Task<IList<T>> GetAllAsync()
        {
            try
            {
                return await db.Set<T>().ToListAsync();
            }
            catch (Exception ex)
            {
                logger.LogError("查询信息失败" + ex.Message);
                throw;
            }
        }

        public async Task<IList<T>> GetAllAsync(Expression<Func<T, bool>> where)
        {
            try
            {
                return await db.Set<T>().Where(where)
                 .ToListAsync();
            }
            catch (Exception ex)
            {
                logger.LogError("条件查询信息失败" + ex.Message);
                throw;
            }
        }

        public async Task<T?> GetAsync(Expression<Func<T, bool>> where)
        {
            try
            {
                return await db.Set<T>().FirstOrDefaultAsync(where);
            }
            catch (Exception ex)
            {
                logger.LogError("单个条件查询信息失败" + ex.Message);
                throw;
            }
        }

        public async Task<int> InsertAsync(T entity)
        {
            try
            {
                await db.Set<T>().AddAsync(entity);
                return  db.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.LogError("添加失败" + ex.Message);
                throw;
            }
        }

        public async Task<int> InsertRangeAsync(IList<T> entities)
        {
            try
            {
                await db.Set<T>().AddRangeAsync(entities);
                return await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                logger.LogError("批量添加失败" + ex.Message);
                throw;
            }

        }


        public async Task<int> UpdateAsync(T entity)
        {
            try
            {
                db.Set<T>().Update(entity);
                return await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                logger.LogError("更改失败" + ex.Message);
                throw;
            }

        }

        public async Task<int> UpdateRangeAsync(IList<T> entities)
        {
            try
            {
                db.Set<T>().UpdateRange(entities);
                return await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                logger.LogError("批量更改失败" + ex.Message);
                throw;
            }

        }
    }
}
